//
//  SQTimeView.m
//  ambition
//
//  Created by 周曼迪 on 2019/12/15.
//  Copyright © 2019 苏青. All rights reserved.
//

#import "SQTimeView.h"
#import "SDAutoLayout.h"

@implementation SQTimeView

- (instancetype)init{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self buildUI];
    }
    return self;
}

- (void)buildUI{
    self.transparentView = [[UIView alloc]init];
    self.transparentView.backgroundColor = [UIColor blackColor];
    self.transparentView.alpha = 0.3;
    [self addSubview:self.transparentView];
    
    self.transparentView.sd_layout.topEqualToView(self).leftEqualToView(self).rightEqualToView(self).bottomEqualToView(self);
    
    UIView * view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    view.sd_layout.bottomSpaceToView(self,W(0)).rightEqualToView(self).leftEqualToView(self).heightIs(W(207)).widthIs(W(375));
    self.datePicke = [[UIDatePicker alloc]init];
    self.datePicke.backgroundColor = [UIColor colorNamed:@"f3Color"];
    [view addSubview:self.datePicke];
    
    self.datePicke.sd_layout.bottomSpaceToView(view,W(0)).rightEqualToView(view).leftEqualToView(view).heightIs(W(173)).widthIs(W(375));
    
    self.determineButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.determineButton setTitle:@"完成" forState:UIControlStateNormal];
    [self.determineButton setTitleColor:[UIColor colorNamed:@"577eb0Color"] forState:UIControlStateNormal];
    self.determineButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.determineButton addTarget:self action:@selector(determineButtonCilck:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.determineButton];
    
    self.determineButton.sd_layout.rightSpaceToView(view,W(15)).topSpaceToView(view,W(10)).heightIs(W(15)).widthIs(W(30));
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor colorNamed:@"333Color"] forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.cancelButton addTarget:self action:@selector(cancelButtonCilck:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.cancelButton];
    
    self.cancelButton.sd_layout.leftSpaceToView(view,W(15)).topSpaceToView(view,W(10)).heightIs(W(15)).widthIs(W(30));
    NSDate * pick = [NSDate date];
    NSDateFormatter * pickMa = [[NSDateFormatter alloc]init];
    [pickMa setDateFormat:@"YYYY-MM-DD"];
    self.datePicke.maximumDate = pick;
    self.datePicke.backgroundColor = [UIColor colorNamed:@"f3Color"];
    [self.datePicke setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    [self.datePicke setDatePickerMode:UIDatePickerModeDate];
    [self.datePicke addTarget:self action:@selector(dataPickerChange:) forControlEvents:UIControlEventValueChanged];
    self.dateTime = [pickMa stringFromDate:pick];
    self.thedateTime = pick;
}


-(void)showWithChooseBlock:(SQtimeViewBlock)timeViewBlock{
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
    self.timeViewBlock = timeViewBlock;
}

- (void)dataPickerChange:(UIDatePicker *)sender{
    NSDate *date = sender.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
    [dateFormatter setDateFormat:@"YYYY-MM-DD"];
    self.dateTime = [dateFormatter stringFromDate:date];
    self.thedateTime = date;
}

- (void)determineButtonCilck:(UIButton *)sender{
    self.timeViewBlock(self.dateTime,self.thedateTime);
    [self removeFromSuperview];
}

- (void)cancelButtonCilck:(UIButton *)sender{
    [self removeFromSuperview];
}

@end
